import {  constantRoutes } from '@/router'
import request from '@/utils/request'
import {processRoute} from '@/utils/menu'
const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
    // sessionStorage.setItem("routes",JSON.stringify(routes)); // 存入sessionStorage中
  }
}


const actions = {
  generateRoutes({ commit }) {
    return new Promise((resolve, reject) => {
        request({
            url: '/menu/user',
            method: 'get'
          }).then(response =>{
            sessionStorage.setItem('menuList',JSON.stringify(response.data))
              const asyncRoutes = processRoute(response.data);
              resolve(asyncRoutes)
          }).catch(error => {
            reject(error)
          })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
